import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';


enum FixType {
  ///左边固定
  left,

  ///右边固定
  right,
}

enum TextWidthType {
  ///固定宽度
  fix,

  ///最大宽度
  max,
}

class KqIconTagView extends StatelessWidget {
  ///边框间距
  final EdgeInsetsGeometry? padding;

  ///总宽度
  final double? width;

  ///总高度
  final double? height;

  ///文本超出后样式,默认为超出省略末尾
  final TextOverflow? overflow;

  ///对于非固定长度的文本，是否强制expand,默认为false
  final bool isExpand;

  ///文本自定义
  final Widget? contentWidget;

  ///竖直对齐方式
  final CrossAxisAlignment crossAxisAlignment;

  ///水平对齐布局
  final MainAxisAlignment mainAxisAlignment;

  ///文案大小默认8
  final double? fontSize;

  ///背景色
  final Color? backgroundColor;

  ///文案颜色
  final Color? textColor;

  ///文本对齐方式
  final TextAlign? textAlign;

  ///最大行高
  final int maxLines;

  ///文案
  final String text;

  ///icon地址
  final String iconPath;

  ///当viewType=3时才会被使用,fixType = FixType.right时，右文本宽度,默认30
  final double? rightWidth;

  ///icon宽默认15,viewType=3时，改宽为左文本宽度
  final double? iconWidth;

  ///icon高默认等于宽
  final double? iconHeight;

  ///icon与text之间的间距。默认6
  final double? spacing;

  ///样式，1:图左文右，2:图右文左，3:文本加文本格式，左边文本的宽高使用icon的宽高，其中宽为最大宽度默认为30
  final int? viewType;

/*** 以下是ViewType=3时左边文本的设置 ****/
  ///当viewType=3时才会被使用，设置左边或者右边固定宽度,默认为左边固定宽度
  final FixType fixType;

  ///当viewType=3时才会被使用，设置左文本iconWidth或者右文本rightWidth的指向最大宽度还是固定宽度
  ///默认宽度指向最大宽度及TextWidthType.max
  final TextWidthType textWidthType;

  ///左边最大行高
  final int leftMaxLines;

  ///左边文本文案
  final String? leftText;

  ///左边文本文字大小
  final double? leftFont;

  ///左边文本文本对齐方式
  final TextAlign? leftTextAlign;

  ///左边文本文字颜色
  final Color? leftTextColor;

  const KqIconTagView({
    Key? key,
    this.padding,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.width,
    this.height,
    this.fixType = FixType.left,
    this.rightWidth,
    this.textWidthType = TextWidthType.max,
    this.overflow = TextOverflow.ellipsis,
    this.maxLines = 1,
    this.leftMaxLines = 1,
    required this.text,
    this.isExpand = false,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.fontSize = 0,
    this.contentWidget,
    this.textAlign,
    this.backgroundColor = KqThemeColors.bgWhite,
    this.textColor = KqThemeColors.text59,
    required this.iconPath,
    this.iconWidth,
    this.iconHeight,
    this.spacing,
    this.viewType = 1,
    this.leftText,
    this.leftFont,
    this.leftTextAlign,
    this.leftTextColor,
  }) : super(key: key);

  Widget rightTextWidget() {
    if (fixType == FixType.left) {
      return rightTextView();
    } else {
      return ConstrainedBox(
        constraints: BoxConstraints(
          maxWidth: rightWidth ?? 30.r,
          minWidth: textWidthType == TextWidthType.fix ? rightWidth ?? 30.r : 0,
        ),
        child: SizedBox(height: iconHeight, child: rightTextView()),
      );
    }
  }

  Widget rightTextView() {
    return Text(
      text,
      maxLines: maxLines,
      overflow: overflow,
      textAlign: textAlign,
      style: TextStyle(
        color: textColor,
        fontSize: fontSize == 0 ? 12.sp : fontSize,
      ),
    );
  }

  Widget iconWidget() {
    return Image.asset(
      iconPath,
      fit: BoxFit.contain,
      height: iconHeight ?? iconWidth ?? 15.r,
      width: iconWidth ?? 15.r,
    );
  }

  ///只有type = 3才会存在
  Widget leftWidget() {
    if (fixType == FixType.left) {
      return ConstrainedBox(
        constraints: BoxConstraints(
          maxWidth: iconWidth ?? 30.r,
          minWidth:
              textWidthType == TextWidthType.fix ? (iconWidth ?? 30.r) : 0,
        ),
        child: SizedBox(height: iconHeight, child: leftTextView()),
      );
    } else {
      return leftTextView();
    }
  }

  Widget leftTextView() {
    return Text(
      leftText ?? '',
      maxLines: leftMaxLines,
      textAlign: leftTextAlign,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
          fontSize: leftFont ?? 12.sp,
          color: leftTextColor ?? KqThemeColors.text26),
    );
  }

  List<Widget> getTagView() {
    List<Widget> view = [];
    if (viewType == 1) {
      view.add(iconWidget());
      view.add(SizedBox(width: spacing ?? 8.r));
      view.add(contentWidget ??
          (isExpand
              ? Expanded(child: rightTextWidget())
              : (width == null
                  ? rightTextWidget()
                  : Expanded(child: rightTextWidget()))));
    } else if (viewType == 2) {
      view.add(contentWidget ??
          (isExpand
              ? Expanded(child: rightTextWidget())
              : (width == null
                  ? rightTextWidget()
                  : Expanded(child: rightTextWidget()))));
      view.add(SizedBox(width: spacing ?? 8.r));
      view.add(iconWidget());
    } else {
      if (fixType == FixType.left) {
        view.add(leftWidget());
        view.add(SizedBox(width: spacing ?? 8.r));
        view.add(contentWidget ??
            (isExpand
                ? Expanded(child: rightTextWidget())
                : (width == null
                    ? rightTextWidget()
                    : Expanded(child: rightTextWidget()))));
      } else {
        view.add(contentWidget ??
            (isExpand
                ? Expanded(child: leftWidget())
                : (width == null
                    ? leftWidget()
                    : Expanded(child: leftWidget()))));
        view.add(SizedBox(width: spacing ?? 8.r));
        view.add(rightTextWidget());
      }
    }
    return view;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding,
      width: width,
      height: height,
      color: backgroundColor,
      child: Row(
        crossAxisAlignment: crossAxisAlignment,
        mainAxisAlignment: mainAxisAlignment,
        children: getTagView(),
      ),
    );
  }
}
